﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SwParaPlatform2
{
    public class TreeViewForm : Form
    {
        private TreeView treeView;

        public TreeViewForm()
        {
            treeView = new TreeView
            {
                Dock = DockStyle.Fill
            };
            Controls.Add(treeView);
        }

        public void LoadDictionaryData(Dictionary<string, object> data)
        {
            treeView.Nodes.Clear();
            foreach (var kvp in data)
            {
                var rootNode = new TreeNode(kvp.Key);
                AddChildNodes(rootNode, kvp.Value);
                treeView.Nodes.Add(rootNode);
            }
        }

        private void AddChildNodes(TreeNode parentNode, object data)
        {
            if (data is Dictionary<string, object> objectDict)
            {
                foreach (var kvp in objectDict)
                {
                    var childNode = new TreeNode(kvp.Key);
                    AddChildNodes(childNode, kvp.Value);
                    parentNode.Nodes.Add(childNode);
                }
            }
            else if (data is Dictionary<string, Dictionary<string, string>> stringDict)
            {
                foreach (var kvp in stringDict)
                {
                    var childNode = new TreeNode(kvp.Key);
                    foreach (var prop in kvp.Value)
                    {
                        var propNode = new TreeNode($"{prop.Key}: {prop.Value}");
                        childNode.Nodes.Add(propNode);
                    }
                    parentNode.Nodes.Add(childNode);
                }
            }
            else if (data is Dictionary<string, string> properties)
            {
                foreach (var prop in properties)
                {
                    var propNode = new TreeNode($"{prop.Key}: {prop.Value}");
                    parentNode.Nodes.Add(propNode);
                }
            }
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // TreeViewForm
            // 
            this.ClientSize = new System.Drawing.Size(398, 391);
            this.Name = "TreeViewForm";
            this.ResumeLayout(false);

        }
    }
}
